package com.cndw.kungfu.model.union;

import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import com.cndw.kungfu.config.ConstantsConfig;
import com.cndw.kungfu.domain.GoodsMode;
import com.cndw.kungfu.ext.XmlProcesser;
import com.cndw.kungfu.model.Context;
import com.cndw.kungfu.vo.goodsvo.GoodsVO;

public class UnionShopConfig {

	/**
	 * 物品id
	 */
	private int goodsId;
	/**
	 * 名字
	 */
	private String name;
	/**
	 * 消耗贡献
	 */
	private int value;
	/**
	 * 要求帮派等级
	 */
	private int level;
	/**
	 * 物品的vo
	 */
	private GoodsVO goodsVO;

	private static final Map<Integer, UnionShopConfig> configMap = new LinkedHashMap<Integer, UnionShopConfig>();

	static {
		load();
	}

	public static synchronized void load() {
		if (configMap.isEmpty()) {
			List<UnionShopConfig> configList = XmlProcesser.processXml(UnionShopConfig.class, ConstantsConfig.getLocaleResource()+"unionShop.xml");
			for (UnionShopConfig config : configList) {
				GoodsMode goodsMode = Context.getGoodsServer().getModeById(config.getGoodsId());
				if (goodsMode == null) {
					continue;
				}
				config.setGoodsVO(goodsMode.toGoodsVO());
				configMap.put(config.getGoodsId(), config);
			}
		}
	}

	public static UnionShopConfig getShopConfig(Integer goodsId) {
		return configMap.get(goodsId);
	}

	public static synchronized void updateConfig() {
		configMap.clear();
		load();
	}

	public int getGoodsId() {
		return goodsId;
	}

	public void setGoodsId(int goodsId) {
		this.goodsId = goodsId;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getValue() {
		return value;
	}

	public void setValue(int value) {
		this.value = value;
	}

	public GoodsVO getGoodsVO() {
		return goodsVO;
	}

	public void setGoodsVO(GoodsVO goodsVO) {
		this.goodsVO = goodsVO;
	}

	public static UnionShopConfig getOne(int goodsId) {
		return configMap.get(goodsId);
	}

	public static Collection<UnionShopConfig> getAll(int level) {
//		List<UnionShopConfig> configs = new ArrayList<UnionShopConfig>();
//		for (UnionShopConfig config : configMap.values()) {
//			if (config.getLevel() <= level) {
//				configs.add(config);
//			}
//		}
		return configMap.values();
	}

	public int getLevel() {
		return level;
	}

	public void setLevel(int level) {
		this.level = level;
	}

	@Deprecated
	public static Map<Integer, UnionShopConfig> getConfigmap() {
		return configMap;
	}

}
